package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.mapper.MerchantMapper;
import com.yx.pojo.Merchant;
import com.yx.pojo.MerchantVO;
import com.yx.pojo.PageDTO;
import com.yx.service.MerchantService;
import com.yx.utils.EmptyUtil;
import org.springframework.stereotype.Service;

@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper,Merchant> implements MerchantService {
    private LambdaQueryWrapper<Merchant> queryWrapper(MerchantVO merchantVO) {
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();
        //商户id
        if (!EmptyUtil.isNullOrEmpty(merchantVO.getMerchantId())) {
            queryWrapper.eq(Merchant::getMerchantId,merchantVO.getMerchantId());
        }
        //商户名称
        if (!EmptyUtil.isNullOrEmpty(merchantVO.getMerchantName())) {
            queryWrapper.like(Merchant::getMerchantName,merchantVO.getMerchantName());
        }
        //商户状态
        if (!EmptyUtil.isNullOrEmpty(merchantVO.getStatus())) {
            queryWrapper.eq(Merchant::getStatus,merchantVO.getStatus());
        }
        //商户入户时间
        if (!EmptyUtil.isNullOrEmpty(merchantVO.getBeginTime()) && !EmptyUtil.isNullOrEmpty(merchantVO.getEndTime())) {
            queryWrapper.ge(Merchant::getEntryDate,merchantVO.getBeginTime())
                    .le(Merchant::getEntryDate,merchantVO.getEndTime());
        }

        return queryWrapper;
    }

    @Override
    public PageDTO<MerchantVO> pageQuery(MerchantVO merchantVO, int pageNum, int pageSize) {
        Page<Merchant> merchantPage = new Page<>(pageNum,pageSize);
        //构建查询条件
        LambdaQueryWrapper<Merchant> queryWrapper = queryWrapper(merchantVO);
        //执行分页查询
        Page<Merchant> page = page(merchantPage, queryWrapper);
        PageDTO<MerchantVO> merchantPageDTO = PageDTO.of(page, MerchantVO.class);
        return merchantPageDTO;
    }

    @Override
    public void update(Merchant merchant) {
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();

        update(merchant,queryWrapper);

    }


}
